<?php

class OPDatabase {

	/**
	* -------------------------------------------------------------------------
	*
	* 									Const
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	* Database host
	*/
	const DB_HOST = 'localhost';
	
	/**
	 * Database name
	 */
	const DB_NAME = 'Octopus';
	
	/**
	*  Database user name
	*/
	const DB_USER = 'root';
	
	/**
	*  Database password
	*/
	const DB_PASSWORD = '';
	
	/**
	* -------------------------------------------------------------------------
	*
	* 									Attributes
	*
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Database
	 */
	protected static $database;
	
	/**
	 * -------------------------------------------------------------------------
	 *
	 * 									Static
	 *
	 * -------------------------------------------------------------------------
	 */
	
	/**
	 * Database connexion
	 */
	public static function connect() {
		OPDatabase::$database = new PDO('mysql:host=' . OPDatabase::DB_HOST . ';dbname=' . OPDatabase::DB_NAME, OPDatabase::DB_USER, OPDatabase::DB_PASSWORD);
				
	}
	
	/**
	 * Clear the database
	 */
	public function clear() {
		$request	= ('SELECT id FROM OP_Player');
		$ids 		= OPDatabase::database()->query($request);
		
		$clearRequest 	= '';
		foreach ($ids->fetchAll() as $id) {
			$clearRequest .= 'DELETE FROM OP_Player WHERE id = '. $id['id'] .'; ';
			
		}

		$request 	= ('SELECT id FROM OP_Rock');
		$ids 		= OPDatabase::database()->query($request);
		
		foreach ($ids->fetchAll() as $id) {
			$clearRequest .= 'DELETE FROM OP_Rock WHERE id = '. $id['id'] .'; ';
			
		}
		
		OPDatabase::database()->query($clearRequest);
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 									Static Getters
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return the database
	 */
	public static function database() {
		return OPDatabase::$database;
		
	}
	
	/**
	* -------------------------------------------------------------------------
	* 								Database Save Requests
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Save a rock into the database
	 * 
	 * @param $rock
	 */
	public static function saveRock($rock) {
		$request = '';
		
		if (null == ($id = $rock->id())) {
			$request = "INSERT INTO `OP_Rock` (`id`, `lat`, `long`, `numberOfPlayersToMove`) 
						VALUES (NULL,
								'". $rock->position()->latitude() ."',
								'". $rock->position()->longitude() ."',
								'". $rock->numberOfPlayersToMove() ."' );";
		
		}else {
			$request = "UPDATE `OP_Rock` SET 
						`lat` = '". $rock->position()->latitude() ."',
						`long`= '". $rock->position()->longitude() ."',
						`numberOfPlayersToMove` = ". $rock->numberOfPlayersToMove() ."
						WHERE `id` = ". $id;
			
		}
				
		OPDatabase::database()->query($request);
		
	}
	
	/**
	* Save a player into the database
	*
	* @param $player
	*/
	public static function savePlayer($player) {
		$request = '';
	
		if (null == ($id = $player->id())) {
			$request = "INSERT INTO `OP_Player` (`id`, `lat`, `long`)
							VALUES (NULL,
									'". $player->position()->latitude() ."',
									'". $player->position()->longitude() ."');";
	
		}else {
			$request = "UPDATE `OP_Player` SET
							`lat` = '". $player->position()->latitude() ."',
							`long`= '". $player->position()->longitude() ."
							 WHERE `id` = ". $id;
				
		}
	
		OPDatabase::database()->query($request);
	
	}
	
	/**
	* -------------------------------------------------------------------------
	* 								Database Select Requests
	* -------------------------------------------------------------------------
	*/
	
	/**
	 * Return all the players in the database
	 * 
	 * @return array<OPPlayer>
	 */
	public function allPlayers() {
		$request = 'SELECT * FROM OP_Player';
		$players = OPDatabase::database()->query($request);
		
		$playersArray = null;
		foreach ($players->fetchAll() as $player) {
			$newPosition 	= OPPosition::newWithLatitudeLongitude($player['lat'], $player['long']);
			$newPlayer		= OPPlayer::newWithIdAndPosition($player['id'], $newPosition);
			
			$playersArray[] = $newPlayer;
			
		}
		
		return $playersArray;
		
	}

}

?> 